<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教练" prop="coachId">
        <el-select v-model="queryParams.coachId" filterable placeholder="请选择教练，可输入搜索" @change="sameChange">
          <el-option
            v-for="item in coachList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['debao:appoint:add']"
        >新增排课
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appointList" @selection-change="handleSelectionChange" :close-on-click-modal="false">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="课程名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.easyCourse.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="教练" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.easyCoach.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约日期" align="center" prop="date"/>
      <el-table-column label="预约用户" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.easyUser.nickName }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="用户名称" align="center" prop="userName" />-->
      <el-table-column label="上课时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.startAt }}~{{ scope.row.endAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status==1">已确认</el-tag>
          <el-tag type="info" v-if="scope.row.status==0">待确认</el-tag>
          <el-tag type="warning" v-if="scope.row.status==-1">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['debao:appoint:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['debao:appoint:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上课预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="选择教练" prop="coachId">
          <el-select v-model="form.coachId" filterable placeholder="请选择教练，可输入搜索" @change="courseChange">
            <el-option
              v-for="item in coachList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择课程" prop="courseId">
          <el-select v-model="form.courseId" filterable placeholder="请选择课程，可输入搜索" @change="courseUserChange">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="userId">
          <el-select v-model="form.appointUserList" filterable multiple placeholder="请选择客户，可输入搜索" @change="userNameChange">
            <el-option
              v-for="item in courseUserList"
              :key="item.user.userId"
              :label="item.user.nickName"
              :value="item.user.userId" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约日期">
          <el-date-picker
            v-model="form.dataList"
            type="dates"
            clearable
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期" @change="dateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间" >
          <el-time-select
            placeholder="上课时间"
            v-model="form.startAt"
            :picker-options="{
             start: '08:00',
              end: '22:00'}">
          </el-time-select>
          <el-time-select
            placeholder="下课时间"
            v-model="form.endAt"
            :picker-options="{start: '08:00',end: '22:00'}"
          >
          </el-time-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppoint, getAppoint, delAppoint, addAppoint, updateAppoint, courseUserList,courselist } from '@/api/debao/appoint'
import { alllistCoach } from '@/api/debao/coach'

export default {
  name: 'Appoint',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上课预约表格数据
      appointList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseId: null,
        coachId: null,
        date: null,
        userId: null,
        userName: null,
        startAt: null,
        endAt: null,
        createAt: null,
        status: null,
        updateAt: null
      },
      // 表单参数
      form: {},
      coachList: [],
      courseUserList: [],
      courseList:[],
      dataList:[],
      dateRange:'',
      // 表单校验
    }
  },
  created() {
    this.getList()
    this.getCoachAlllist()
  },
  methods: {
    /** 查询上课预约列表 */
    getList() {
      this.loading = true
      listAppoint(this.queryParams).then(response => {
        this.appointList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /**
     * 获取教练列表
     */
    getCoachAlllist() {
      alllistCoach().then(response => {
        this.coachList = response.data
      })
    },
    /**
     * 获取报名用户列表
     * @param
     */
    courseUserChange(e){
      courseUserList(e).then(response => {
        this.courseUserList = response.data
      })
    },
    /**
     * 选择日期
     */
    dateChange(dates){
      // 对日期字符串数组进行排序
      const sortedDates = dates.sort((a, b) => {
        const dateA = new Date(a);
        const dateB = new Date(b);
        return dateA - dateB;
      });
      // 更新排序后的日期数组
      this.dateRange = sortedDates[0]+'~'+sortedDates[sortedDates.length - 1];
    },

    /**
     * 选择课程
     */
    courseChange(e) {
      courselist(e).then(response => {
        this.courseList = response.data
      })
    },

    //选中的普通用户
    sameChange() {
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        courseName: '',
        courseId: '',
        coachId: '',
        dateList: [],
        appointUserList: [],
        startAt: '',
        endAt: '',
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加上课预约'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAppoint(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改上课预约'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppoint(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAppoint(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除上课预约编号为"' + ids + '"的数据项？').then(function() {
        return delAppoint(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
  }
}
</script>
